package com.gko100.service.impl;

import java.util.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;



import com.gko100.dao.AdministratorDao;

import com.gko100.dao.ArticleDao;
import com.gko100.dao.CategoryDao;

import com.gko100.dao.AdminPermDao;
import com.gko100.dao.DiseaseDao;
import com.gko100.dao.ImageDao;
import com.gko100.dao.LanguageDao;
import com.gko100.dao.MessageDao;
import com.gko100.dao.NavigationDao;
import com.gko100.dao.PermissionDao;
import com.gko100.dao.RemarkDao;
import com.gko100.dao.UserDao;
import com.gko100.dao.UserPermDao;
import com.gko100.dao.UserRmkDao;
import com.gko100.model.AdminPerm;
import com.gko100.model.Administrator;
import com.gko100.model.Article;
import com.gko100.model.Category;
import com.gko100.model.Disease;
import com.gko100.model.Image;
import com.gko100.model.Language;
import com.gko100.model.Message;
import com.gko100.model.Navigation;
import com.gko100.model.Permission;
import com.gko100.model.User;
import com.gko100.model.UserPerm;
import com.gko100.service.AdminManager;
import com.gko100.util.DateTimeUtil;
import com.gko100.util.MD5;

public class AdminManagerImpl implements AdminManager {

	private AdministratorDao administratorDao;
	private CategoryDao categoryDao;
	private PermissionDao permissionDao;
	private AdminPermDao adminPermDao;
	private ImageDao imageDao;
	private NavigationDao navigationDao;
	private ArticleDao articleDao;
	private RemarkDao remarkDao;
	private UserDao userDao;
	private UserPermDao userPermDao;
	private LanguageDao languageDao;
	private UserRmkDao userRmkDao;
	private MessageDao messageDao;
	private DiseaseDao diseaseDao;
	
	public DiseaseDao getDiseaseDao() {
		return diseaseDao;
	}

	public void setDiseaseDao(DiseaseDao diseaseDao) {
		this.diseaseDao = diseaseDao;
	}

	
	
	public AdministratorDao getAdministratorDao() {
		return administratorDao;
	}

	public void setAdministratorDao(AdministratorDao administratorDao) {
		this.administratorDao = administratorDao;
	}

	public CategoryDao getCategoryDao() {
		return categoryDao;
	}

	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public PermissionDao getPermissionDao() {
		return permissionDao;
	}

	public void setPermissionDao(PermissionDao permissionDao) {
		this.permissionDao = permissionDao;
	}

	public AdminPermDao getAdminPermDao() {
		return adminPermDao;
	}

	public void setAdminPermDao(AdminPermDao adminPermDao) {
		this.adminPermDao = adminPermDao;
	}

	public ImageDao getImageDao() {
		return imageDao;
	}

	public void setImageDao(ImageDao imageDao) {
		this.imageDao = imageDao;
	}

	public NavigationDao getNavigationDao() {
		return navigationDao;
	}

	public void setNavigationDao(NavigationDao navigationDao) {
		this.navigationDao = navigationDao;
	}

	public ArticleDao getArticleDao() {
		return articleDao;
	}

	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}

	public RemarkDao getRemarkDao() {
		return remarkDao;
	}

	public void setRemarkDao(RemarkDao remarkDao) {
		this.remarkDao = remarkDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public UserRmkDao getUserRmkDao() {
		return userRmkDao;
	}

	public void setUserRmkDao(UserRmkDao userRmkDao) {
		this.userRmkDao = userRmkDao;
	}
	public LanguageDao getLanguageDao() {
		return languageDao;
	}

	public void setLanguageDao(LanguageDao languageDao) {
		this.languageDao = languageDao;
	}

	public int getPageCount(int count, int pageSize) {
			
		return (count+pageSize-1)/pageSize;
	}
	public int getImageCount(){
		return imageDao.getImageCount();
	}

	public int getAdminCount(){
		
		return administratorDao.getAdminCount();
	}


	public UserPermDao getUserPermDao() {
		return userPermDao;
	}

	public void setUserPermDao(UserPermDao userPermDao) {
		this.userPermDao = userPermDao;
	}

	public MessageDao getMessageDao() {
		return messageDao;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	/*
	 * 登录、退出
	 */
	public boolean validLogin(Administrator admin) {
		boolean flag=false;
		if(admin!=null){
			
			String name = admin.getName();
			String password = admin.getPassword();
			if(name!=null&&password!=null){
				
				Administrator a= administratorDao.findAdminByNameAndPass(name, MD5.encoderByMd5(password));
				if(a!=null){
					
					a.setSignState("login");
			        a.setLastSignDate(DateTimeUtil.getNowTime());
					administratorDao.update(a);
					flag=true;
				}
			}
		}
		
		return flag;
	}
	public void logout(Administrator admin) {
		
		admin.setSignState("logout");
		administratorDao.update(admin);
	}
	
	
	//权限管理

	public void add_perm(Permission perm){
		
		permissionDao.add_perm(perm);
	}
	public void del_permByPermId(Integer permId){
		
		Permission perm = permissionDao.findPermByPermId(permId);
		permissionDao.del_perm(perm);
	}
	public void modify_permByPermId(Integer permId) {
		Permission perm = permissionDao.findPermByPermId(permId);
		permissionDao.modify_perm(perm);
		
	}
	public List<Permission> findPermList(){
		
		return permissionDao.findPermList();

	}
		
	
	/*
	 * 管理员信息管理
	 */
	
	public void add_admin(Administrator admin) {
		
		administratorDao.save(admin);
		
	}

	public void del_adminByAdminId(Integer adminId) {
		
		Administrator admin = administratorDao.findAdminByAdminId(adminId);
		administratorDao.delete(admin);
		
	}

	public void modify_adminByAdmin(Administrator admin) {
		
		administratorDao.update(admin);
		
	}
	
	public Administrator findAdminByName(String name) {
		
		return administratorDao.findAdminByName(name);
		
	}

	public Administrator findAdminByNameAndPass(String name, String password) {
		
		return administratorDao.findAdminByNameAndPass(name, password);
	}

	public List<Administrator> findAdminListAllByPage(int pageNo) {
		
		return administratorDao.findAdminAll(pageNo,LIST_PAGE_SIZE);
	}
	public Administrator findAdminByAdminId(Integer adminId){
		
		return administratorDao.findAdminByAdminId(adminId);
	}
	
	/*
	 * 管理员权限处理
	 */
	public List<AdminPerm> findAdminPermListByAdminId(Integer adminId){
		
		Administrator admin = administratorDao.findAdminByAdminId(adminId);
		return adminPermDao.findAdminPermListByAdmin(admin);
	}
	
	public void add_adminPerm(Administrator admin){
	
		List<Permission> permList = permissionDao.findPermList();
		System.out.println("adminId============"+admin.getAdminId()+"name========"+admin.getName()+"note========"+admin.getAdminNote());
		for(int i=0; i<permList.size(); i++){

			System.out.println(permList.get(i).getPermName());
			
		}
		for(int i=0; i<permList.size(); i++){
			
			AdminPerm adminPerm = new AdminPerm();
			adminPerm.setAdmin(admin);
			adminPerm.setPerm(permList.get(i));
			adminPerm.setPermValue(false);
			System.out.println("admin======="+adminPerm.getAdmin()+"------perm===="+adminPerm.getPerm()+"------permValue===="+adminPerm.getPermValue());
			adminPermDao.add_adminPerm(adminPerm);
		}
	}
	
	public void del_adminPermByAdminId(Integer adminId){
		
		Administrator admin = administratorDao.findAdminByAdminId(adminId);
		List<AdminPerm> adminPermList = adminPermDao.findAdminPermListByAdmin(admin);
		for(int i=0; i<adminPermList.size();i++){
			adminPermDao.del_adminPerm(adminPermList.get(i));
		}
	}
	
	public void modify_adminPermByAdminPermId(int adminId,Integer[] permIdList){
		Administrator admin = administratorDao.findAdminByAdminId(adminId);
		for(int i=0;i<permIdList.length;i++){
			System.out.println(permIdList[i]);
			Permission perm = permissionDao.findPermByPermId(permIdList[i]);
			AdminPerm adminPerm = adminPermDao.findAdminPermByAdminAndPerm(admin,perm);
			adminPerm.setPermValue(true);
			System.out.println("adminPermId========"+adminPerm.getAdminPermId()+"-----adminPermValue========="+adminPerm.getPermValue());
			adminPermDao.modify_AdminPerm(adminPerm);
		}
		
	}
	public AdminPerm findAdminPermByAdminPermId(Integer adminPermId){
		
		return adminPermDao.findAdminPermByAdminPermId(adminPermId);
		
	}
	
	/*
	 * ---------------会员信息模块---------------
	 * ---------------包括会员信息管理、会员信息添加、会员权限3部分
	 */
	
	public void add_user(User user){
		
		userDao.save(user);
		
	}
	public void modify_user(User user){
		
		userDao.update(user);
	}
	public void del_user(User user){
		
		userDao.delete(user);
	}
	public User findUserById(Integer userId){
		
		User user = userDao.findUserByUserId(userId);
		if(user!=null){
			return user;
		}else{
			System.out.println(this.getClass().getName()+"---"+new Exception().getStackTrace()[0].getMethodName()+"---user======null");
			return null;
		}
		
	}
	public List<User> findUserAll(){
		
		List<User> userList = userDao.findUserAll();
		if(userList!=null&&userList.size()>=1){
			
			return userList;
		}else{
			
			System.out.println(this.getClass().getName()+"---"+new Exception().getStackTrace()[0].getMethodName()+"---userList======null");
			return null;
		}
	}
	public void add_userPerm(UserPerm userPerm){
		
		userPermDao.add_userPerm(userPerm);
	}
	public void add_userPermByUser(User user){
		
		List<Permission> permList = permissionDao.findPermList();
		if(permList!=null){
			for(int i=0; i<permList.size(); i++){
				
				UserPerm userPerm = new UserPerm();
				userPerm.setUser(user);
				userPerm.setPerm(permList.get(i));
				userPerm.setPermValue(false);
				userPermDao.add_userPerm(userPerm);
			}
		}
		
		
	}
	public void modify_userPermByUser(User user, Integer[] permIdList){
		
		
		
	}
	public void del_userPermByUser(User user){
		
		
	}
	public List<UserPerm> findUserPermListByUser(User user){
		
		return null;
	}
	
	
	/*
	 *  -------------------系统模块处理------------------                        
	 *  -------------------包括用户中心、系统管理、网站管理、留言信箱4部分
	 */
	
	public void add_cate(Category cate){
		
		categoryDao.save(cate);
	}
	public void del_cate(Category cate){
		
		categoryDao.delete(cate);
		
	}
	public void modify_cate(Category cate){
		
		categoryDao.update(cate);
		
	}
	public Category findCateByCateId(Integer cateId){
		
		Category cate = categoryDao.findCateByCateId(cateId);
		if(cate!=null){
			return cate;
		}else{
			
			return null;
		}
		
	}
	
	public Category findCateByCateNote(String cateNote){
		
		Category cate = categoryDao.findCateByCateNote(cateNote);
		if(cate!=null){
			return cate;
		}else{
			
			return null;
		}
	}
	
	public List<Category> findCateListByCatePid(Integer catePid){
		
		List<Category> cateList = categoryDao.findCateListByCatePid(catePid);
		if(cateList!=null&&cateList.size()>=1){
			return cateList;
		}else{
			
			return null;
		}
	}
	public List<Category> findCateAll(){
		
		List<Category> cateList = categoryDao.findCateAll();
		if(cateList!=null&&cateList.size()>=1){
			return cateList;
		}else{
			
			return null;
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	/*
	 * 导航信息管理
	 * @see com.gko100.service.AdminManager#findNavByNavId(java.lang.Integer)
	 */
	public void add_nav(Navigation nav) {
		
		navigationDao.saveNav(nav);
		
	}
	public void del_nav(Navigation nav) {
		
		navigationDao.delNav(nav);
		
	}
	
	public List<Navigation> findNavListByCate(Category cate){
		
		return navigationDao.findNavListByCate(cate);
		
	}
	
	public Navigation findNavByNavNote(String navNote){
		
		return navigationDao.findNavByNavNote(navNote);
		
	}
	
	public Navigation findNavByCate(Category cate){
		
		return navigationDao.findNavByCate(cate);
		
	}
	
	public Navigation findNavByNavId(Integer navId) {
		
		return navigationDao.findNavByNavId(navId);
	}
	public Navigation findNavByNavName(String navName) {
		
		
		return navigationDao.findNavByNavName(navName);
	}
	public List<Navigation> findNavListByNavPId(Integer navPid) {
		
		return navigationDao.findNavListByNavPid(navPid);
		
	}
	
	
	
	public List<Navigation> findNavListByNavPIdAndLangId(Integer navPid,Integer langId){
		
	
		Language lang = languageDao.findLangyLangId(langId);
		List<Navigation> navList = navigationDao.findNavListByNavPIdAndLang(navPid, lang);
		
		if(navList!=null){
			return navList;
		}else{
			return null;
			
		}
		
	}
	
	public void modify_navByNav(Navigation nav) {
		
		navigationDao.updateNav(nav);
		
	}
	
	

	//图片管理
	public void cdel_imageByImage(Image image){
		
		if(image!=null){
			
			imageDao.delete(image);
		}
		
	}
	
	public void del_imageByImage(Image image) {
		if(image!=null){
			image.setDeleteFlag(true);
			imageDao.update(image);
		}
		
		
	}

	public List<Image> findImgListByNav(Navigation nav) {
		
		return imageDao.findImageListByNav(nav);
		
	}
	public Image findImgByImgId(Integer imgId) {
		
		return imageDao.findImageByImgId(imgId);
	}
	
	public void modify_imageByImage(Image image) {
		
		imageDao.update(image);
		
	}
	public void add_image(Image image) {
		
		imageDao.save(image);
		
	}
	public List<Image> findImgAll(){
		
		return imageDao.findImageAll();
	}
	
	public List<Image> findImageListAllByPage(int pageNo){
		
		return imageDao.findImageAll(pageNo, LIST_PAGE_SIZE);
		
	}
	
	public List<Image> findImageListDelByArti(Article arti){
		
		return imageDao.findImageListDelByArti(arti);
	}
	
	public List<Image> findImageByArti(Article arti){
		
		return imageDao.findImageListByArti(arti);
		
	}
	
	//文章信息管理
	/* 
	 * {见接口方法说明}
	 */
	public void add_arti(Article arti) {
		
		Date now = new Date();
		String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
        Timestamp timestamp = Timestamp.valueOf(nowTime);
        arti.setAddDate(timestamp);
		articleDao.save(arti);
		
	}
	/* 
	 * {见接口方法说明}
	 */
	public void del_arti(Article arti) {
		
		arti.setDeleteFlag(true);
		articleDao.update(arti);
		
	}
	
	public void cdel_arti(Article arti){
		
		articleDao.delete(arti);
		
	}
	/* 
	 * {见接口方法说明}
	 */
	public Article findBackArtiByArtiId(Integer artiId){
		
		return articleDao.findBackArtiByArtiId(artiId);
	}
	
	public Article findArtiByArtiId(Integer artiId) {
		
		
		return articleDao.get(artiId);
	}
	/* 
	 * {见接口方法说明}
	 */
	public List<Article> findArtiListByNav(Navigation nav,int pageNo) {
		
		return articleDao.findArtiListByNav(nav,pageNo,LIST_PAGE_SIZE);
	}
	
	public int getArtiCount(Navigation nav){
		
		return articleDao.getArtiCount(nav);
	}
	
	public Navigation findNavByArtiId(Integer artiId){
		
		Navigation nav = articleDao.findNavByArtiId(artiId);
		if(nav!=null){
			return nav;
		}else{
			
			return null;
		}
	}
	
	public void modify_arti(Article arti) {
		
		articleDao.update(arti);
		
	}
	public long find_backArtiTotalCountByNav(List<Navigation> navList){
		return articleDao.find_backArtiTotalCountByNav(navList);
	}
	
	//前台---分类文章总数
	public long find_artiTotalCountByNav(List<Navigation> navList){
		
		return articleDao.find_artiTotalCountByNav(navList);
		
	}
	public List<Article> findBackArtiListByNavAndPage(List<Navigation> navList, int offset, int pageSize){
		
		return articleDao.findBackArtiListByNavAndPage(navList, offset, pageSize);
		
	}
	
	//前台---查找分类下所有文章---分页
	public List<Article> findArtiListByNavListAndPage(List<Navigation> navList, int offset, int pageSize){
		
		return articleDao.findArtiListByNavListAndPage(navList, offset, pageSize);
		
	}
	
	public List<Article> findArtiFront(Navigation nav, Integer artiId, int offset, int pageSize){
		
		return articleDao.findArtiFront(nav, artiId, offset, pageSize);
	}
	public List<Article> findArtiNext(Navigation nav, Integer artiId, int offset, int pageSize){
		return articleDao.findArtiNext(nav, artiId, offset, pageSize);
	}
	public List<Article> findArtiListByNavAndPage(Navigation nav, int offset, int pageSize){
		
		return articleDao.findArtiListByNavAndPage(nav, offset, pageSize);
	}
	
	public List<Article> findArtiFrontAndNext(Navigation nav, Integer artiId, int offset, int pageSize){
		
		return articleDao.findArtiListByNavAndPage(nav, offset, pageSize);
		
	}
	
	public List<Article> findArtiListByNav(Navigation nav) {
		
		return articleDao.findArtiListByNav(nav);
	}

	
	
	public List<Article> findArtiListByNavAndSortParam(Navigation nav, String sortParam){
		
		return articleDao.findArtiListByNavAndSortParam(nav, sortParam);
		
	}
	
	public List<Article> findArtiListByDis(Disease disease){
		
		return articleDao.findArtiListByDise(disease);
		
	}
	
	public List<Article> findArtiListByDisAndNav(List<Disease> diseList, Navigation nav){
		
		
		return articleDao.findArtiListByDisAndNav(diseList, nav);
		
	}
	
	public List<Article> findArtiListByDisAndNote(Disease dise, String note){
		
		return articleDao.findArtiListByDisAndNote(dise, note);
		
	}
	
	public List<Article> findArtiListByDisListAndNote(List<Disease> diseList, String note){
		
		return articleDao.findArtiListByDisListAndNote(diseList, note);
		
	}
	
	public List<Article> findArtiListByNavAndLang(Navigation nav, Language lang, String sortParam){
		
		List<Article> artiList = articleDao.findArtiListByNavAndLang(nav, lang, sortParam);
			
		if(artiList!=null&&artiList.size()>=1){
			return artiList;
		}else{
			
			return null;
		}
	}
	public List<Disease> findDiseListByArticle(Article arti){
		
		return articleDao.findDiseListByArticle(arti);
	}

	
	
	public Language findLangByLangId(Integer langId){
		
		return languageDao.findLangyLangId(langId);
		
	}

	public Language findLangByLangName(String langName){
		
		Language lang = languageDao.findLangByLangName(langName);
		if(lang!=null){
			return lang;
		}
		return null;
		
	}
	public List<Language> findLangListAll(){
		
		List<Language> langList = languageDao.findLangListAll();
		if(langList!=null&&langList.size()>=1){
			
			return langList;
		}
		return null;
		
	}

	
	/*
	 *  -------------------留言信息模块------------------                        
	 *  -------------------包括留言管理1部分
	 */
	
	public Message findMessageByMId(Integer mId){
		
		Message message = messageDao.findMessageByMId(mId);
		if(message!=null){
			return message;
		}else{
			return null;
		}
		
	}
	
	public void add_message(Message message){
		
		messageDao.save(message);
	}

	public void modify_message(Message message){
		
		messageDao.update(message);
	}
	
	public void del_message(Message message){
		
		messageDao.delete(message);
	}
	
	public List<Message> findMessageAll(){
		
		List<Message> mList = messageDao.findMessageAll();
		if(mList!=null){
			return mList;
		}else{
			return null;
		}
		
	}

	public long find_totalCountBySearchKey(String searchKey){
		
		return articleDao.find_totalCountBySearchKey(searchKey);
		
	}
	public List<Article> find_artiListBySearchKey(String searchKey){
		
		List<Article> artiList = articleDao.find_artiListBySearchKey(searchKey);
		if(artiList!=null){
			
			return artiList;
		}else{
			return null;
		}

	}
	
	public List<Article> find_artiListBySearchKeyAndPage(String searchKey, int offset, int pageSize){
		
		return articleDao.find_artiListBySearchKeyAndPage(searchKey, offset, pageSize);

	}
	
	/*
	 *  -------------------疾病分类模块------------------                        
	 *  -------------------
	 */
	
	public List<Disease> findDisListByDisPid(Integer disPid){
		
		List<Disease> disList = diseaseDao.findDisListByDisPid(disPid);
		if(disList!=null&&disList.size()>=1){
			
			return disList;
		}
		return null;
		
	}
		
	public List<Disease> findDisListByDisPidAndLang(Integer disPid,Language lang) {
		
		List<Disease> disList = diseaseDao.findDisListByDisPidAndLang(disPid, lang);
		if(disList!=null&&disList.size()>=1){
			
			return disList;
		}
		return null;
	}
	
	public Disease findDisByDisId(Integer diseaseId) {
		
		Disease disease = diseaseDao.findDisByDisId(diseaseId);
		
		if(disease!=null){
			return disease;
		}
		return null;
	}
	
	public Disease findDisByTitle(String title) {
		
		Disease disease = diseaseDao.findDisByTitle(title);
		if(disease!=null){
			return disease;
		}
		return null;
	}
	
	public void add_disease(Disease dis) {
	
		diseaseDao.saveDisease(dis);
	}
	
	public void del_disease(Disease dis) {
	
		diseaseDao.delDisease(dis);
	}
	
	public void modify_disease(Disease dis) {
	
		diseaseDao.updateDisease(dis);
	}

	
	
	


		
}
